// INCLUDES
#include "logomatic_v2.h"
#include "LPC214x.h"
#include "main.h"

// FUNCTIONS

void LogomaticV2Blinky(void)
{
   while(1)
   {
      LogomaticV2Stat(0, LOGOMATIC_STAT_ON);
      LogomaticV2Stat(1, LOGOMATIC_STAT_OFF);
      delay_ms(500);
      LogomaticV2Stat(0, LOGOMATIC_STAT_OFF);
      LogomaticV2Stat(1, LOGOMATIC_STAT_ON);
      delay_ms(500);
   }
} // LogomaticV2Blinky

void LogomaticV2Stat  (
                      const uint32_t statNum, // in
                      const uint32_t onOff    // in
                      )
{
        switch (statNum)
        {
            case STAT0:
		if(LOGOMATIC_STAT_ON == onOff) P0CLR = (1<<STAT0);
		else  P0SET = (1<<STAT0);
                break;
            case STAT1:
		if(LOGOMATIC_STAT_ON == onOff) P0CLR = (1<<STAT1);
		else  P0SET = (1<<STAT1);
                break;
            case STAT2:
		if(LOGOMATIC_STAT_ON == onOff) P0SET = (1<<STAT2);
		else  P0CLR = (1<<STAT2);
                break;
            default:
                break;
        }
} // LogomaticV2Stat


void logomaticPWM(void)
{
    
    int	i,j;
    PINSEL1 = 0x00000400;       //p0.21 = PWM5
    PWMTCR = 0x02;              //pwm reset
    PWMPR = 0;                  //prescaler disable
    PWMPCR = 0x2000;            //PWM5 enabled - single edge
    PWMMCR = 0x00000002;        //reset on PWMMR0
    PWMMR0 = 0x0000ffff;        //set pwm frequency
    PWMMR5 = 0x000000ff;        //set pwm5 duty cycle
    PWMLER = 0x21;              //latch new match entries
    PWMTCR = 0x09;              //enable PWM
    
    while(1)
    {
        for(i=0x0;i<0x7fff;i+=10)
        {
            PWMMR5 = i;
            PWMLER = 0x20;
            for(j=0;j<0x7ff;j++);
        }
        for(i=0x7fff;i>0;i-=10)
        {
            PWMMR5 = i;
            PWMLER = 0x20;
            for(j=0;j<0x7ff;j++);
        }
    }
}